home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / test / painttes.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  3.6 KB  |  159 lines

  1. /*
  2.  * @(#)PaintTest.java    1.20 95/09/16 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. import java.awt.*;
  20.  
  21. public class PaintTest extends Frame {
  22.     public PaintTest() {
  23.     setTitle("PaintTest");
  24.     setBackground(Color.lightGray);
  25.     setLayout(new BorderLayout());
  26.     add("Center", new PaintPanel());
  27.     reshape(0, 0, 300, 300);
  28.     show();
  29.     }
  30.         
  31.     public static void main(String args[]) {
  32.     new PrintTest(new PaintTest());
  33.     }
  34. }
  35.  
  36. class PaintPanel extends Panel {
  37.     public PaintPanel() {
  38.     setLayout(new BorderLayout());
  39.     Panel grid = new Panel();
  40.     grid.setLayout(new GridLayout(0, 1));
  41.     add("Center", grid);
  42.     grid.add(new PaintCanvas());
  43.     grid.add(new PaintCanvas());
  44.     grid.add(new PaintCanvas());
  45.     reshape(0, 0, 20, 20);
  46.     }
  47. }
  48.  
  49. class PaintCanvas extends Canvas {    
  50.     int     hadd = 0;
  51.     int        xadd = 0;
  52.     int        yadd = 0;
  53.     boolean    focus = false;
  54.  
  55.     public PaintCanvas() {
  56.     reshape(0, 0, 100, 100);
  57.     }
  58.  
  59.     public boolean gotFocus(Event e, Object arg) {
  60.     focus = true;
  61.     repaint();
  62.     return true;
  63.     }
  64.  
  65.     public boolean lostFocus(Event e, Object arg) {
  66.     focus = false;
  67.     repaint();
  68.     return true;
  69.     }
  70.  
  71.     public void paint(Graphics g) {
  72.     Rectangle r = bounds();
  73.     int hlines = r.height / 10;
  74.     int vlines = r.width / 10;
  75.  
  76.     g.setColor(Color.pink);
  77.     for (int i = 1; i <= hlines; i++) {
  78.         g.drawLine(0, i * 10, r.width, i * 10);
  79.     }
  80.     for (int i = 1; i <= vlines; i++) {
  81.         g.drawLine(i * 10, 0, i * 10, r.height);
  82.     }
  83.     if (focus) {
  84.         g.setColor(Color.red);
  85.     } else {
  86.         g.setColor(Color.darkGray);
  87.     }
  88.     g.drawRect(0, 0, r.width-1, r.height-1);
  89.     g.drawLine(0, 0, r.width, r.height);
  90.     g.drawLine(r.width, 0, 0, r.height);
  91.     g.drawLine(0, r.height / 2, r.width, r.height / 2);
  92.     g.drawLine(r.width / 2, 0, r.width / 2, r.height);
  93.  
  94.     for (int i = 0; i < 360; i += 30) {
  95.         float percent = (((float)i /(float)360) * (float)255);
  96.         g.setColor(new Color((int)percent, (int)(percent / 2),
  97.                       (int)percent));
  98.         g.fillArc(xadd, yadd, 100 + hadd, 100 + hadd, i, 30);
  99.     }
  100.     }
  101.  
  102.     public boolean handleEvent(Event e) {
  103.     switch (e.id) {
  104.       case Event.KEY_ACTION:
  105.       case Event.KEY_PRESS:
  106.         switch (e.key) {
  107.           case Event.UP:
  108.         yadd -= 5;
  109.         repaint();
  110.         return true;
  111.           case Event.DOWN:
  112.         yadd += 5;
  113.         repaint();
  114.         return true;
  115.           case Event.RIGHT:
  116.           case 'r':
  117.         xadd += 5;
  118.         repaint();
  119.         return true;
  120.           case Event.LEFT:
  121.         xadd -= 5;
  122.         repaint();
  123.         return true;
  124.           case Event.PGUP:
  125.         hadd += 10;
  126.         repaint();
  127.         return true;
  128.           case Event.PGDN:
  129.         hadd -= 10;
  130.         repaint();
  131.         return true;
  132.           default:
  133.         return false;
  134.         }
  135.  
  136.       default:
  137.         return super.handleEvent(e);
  138.     }
  139.     }
  140.  
  141. }
  142.  
  143. class PrintTest extends Frame {
  144.     Component comp;
  145.  
  146.     public PrintTest(Component comp) {
  147.     this.comp = comp;
  148.     reshape(400, 0, 400, 400);
  149.     show();
  150.     }
  151.  
  152.     public void paint(Graphics g) {
  153.     Dimension d = size();
  154.     g.setColor(getBackground());
  155.     g.fillRect(0, 0, d.width, d.height);
  156.     comp.printAll(g);
  157.     }
  158. }
  159.